#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
# File Name: dynamic_menu.sh
# Author: GID5564
# Description: 动态菜单文件items.sh
# Created Time: 16/10/23-18:12:01
#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
#! /usr/bin/env bash

#新建一sh文件，并写入文件头
function WriteShellHead(){
    local file=$1 #自定义文件名称
    local description=$2 #文件描述
    local modi_time=$(date +%D-%T)
    
    NewFile ${file}
    wtf "#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※" ${file}
    wtf "# File Name: ${file##*/}" ${file}
    wtf "# Author: GID5564" ${file}
    wtf "# Description: ${description}"  ${file}
    wtf "# Created Time: ${modi_time}"  ${file}
    wtf "#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※" ${file}
    wtf " "  ${file}
    wtf "#! /usr/bin/env bash"  ${file}
    wtf " "  ${file}
    
    unset file
    unset description
    unset modi_time
}

function wtf(){
    local str=$1
    local file=$2
    if [[ -z ${file} ]];then
        file=${DYNAMIC_PAYH}/items.sh
    fi
    WriteToFile ${file} ${str}
    unset str
    unset file
}


#python项目菜单
function WriteItemMenu(){
    local SECTION='item'
    local KEYS=()
    local value
    local count=1
    local key
    
    wtf 'function python_item_menu() {'
    wtf '	RETURN_TO_WHERE="python_item_menu"'
    wtf '	local MENU_TITLE="Python项目依赖安装"'
    wtf '	local P_OPTION=$("${WINDOW:-dialog}" --title "${MENU_TITLE}" --menu "${CURRENT_SYSTEM}"	--cancel-button "退出" 0 50 0 \\'
    
    KEYS=$(get_keys ${SECTION})
    KEYS=(`echo ${KEYS}|grep "[A-Za-z0-9_]" |awk -v RS='' '{gsub(" "," "); print}'`)

    for key in ${KEYS}
    do
        local value=$(read_ini ${SECTION} ${key})
        local title=${value##*/}
        local isn=$(IsEmpty ${title})
        if [[ ${isn} -eq 1 ]];then
            wtf "		\"${count}\" \"${title}\" \\"
            let count++
        fi
        unset value
        unset title
    done
    
    
    wtf '		"0" "◆返回上一级菜单" \\
		3>&1 1>&2 2>&3)'
    wtf '	#############'
    wtf '	case ${P_OPTION} in'
    wtf '	0 | "")  RETURN_TO_WHERE="basic_menu" ;;'
    count=1
    for key in ${KEYS}
    do
        wtf "    ${count}) InstallItemPkgToEnv \"${key}\" ;;"
        let count++
    done
    
    wtf '	esac'
    wtf '	###############'
    wtf '	press_return_to_where'
    wtf '}'
    wtf ''
    
    unset SECTION
    unset KEYS
    unset value
    unset count
    unset key
    
}

#网络工具菜单
function WebsiteMenu(){
    local SECTION='website'
    local KEYS=()
    local count=1
    local key
    
    wtf 'function open_website_menu() {'
    wtf '	RETURN_TO_WHERE="open_website_menu"'
    wtf '	local MENU_TITLE="web 学习工具"'
    wtf '	local P_OPTION=$("${WINDOW:-dialog}" --title "${MENU_TITLE}" --menu "${CURRENT_SYSTEM}"	--cancel-button "退出" 0 50 0 \\'
    
    KEYS=$(get_keys ${SECTION})
    KEYS=(`echo ${KEYS}|grep "[A-Za-z0-9_]" |awk -v RS='' '{gsub(" "," "); print}'`)

    for key in ${KEYS}
    do
        wtf "		\"${count}\" \"${key}\" \\"
        let count++
    done
    
    
    wtf '		"0" "◆返回上一级菜单" \\
		3>&1 1>&2 2>&3)'
    wtf '	#############'
    wtf '	case ${P_OPTION} in'
    wtf '	0 | "")  RETURN_TO_WHERE="basic_menu" ;;'
    count=1
    
    for key in ${KEYS}
    do
        local value=$(read_ini ${SECTION} ${key})
        wtf "    ${count}) open_website \"${value}\" ;;"
        let count++
        unset value
    done
    
    wtf '	esac'
    wtf '	###############'
    wtf '	press_return_to_where'
    wtf '}'
    wtf ''
    
    unset SECTION
    unset KEYS
    unset count
    unset key
    
}



function SetDynamicMenu(){
    local python_item="${DYNAMIC_PAYH}/items.sh"
    if [[ ! -d ${DYNAMIC_PAYH} ]];then
        mkdir -p ${DYNAMIC_PAYH}
    fi
    if [[ -f ${python_item} ]];then
        mv -f ${python_item} ${DYNAMIC_PAYH}/items_old.sh
    fi
    #shell文件头
    WriteShellHead ${python_item}
    #动态Python项目菜单
    WriteItemMenu
    #网络工具菜单
    WebsiteMenu
    source ${python_item}
    unset python_item
}
























